import { _decorator, Color, color, Component, Node, Prefab, Sprite, SpriteFrame, Vec3 } from 'cc';
import { CharacterWearType, GameData } from './GameData';
import { PoolManager } from '../../Scripts/Framework/Managers/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('Character')
export class Character extends Component {

    @property([Sprite])
    skins: Sprite[] = [];

    @property([Sprite])
    wears: Sprite[] = [];

    id: number;

    start() {

    }

    update(deltaTime: number) {

    }

    static Init(parent: Node): Character {
        let node: Node = PoolManager.GetNodeByPrefab(GameData.prefabsBundle.get("Character", Prefab), parent);
        node.setPosition(Vec3.ZERO);
        return node.getComponent(Character);
    }

    Init(data) {
        this.id = data.id;
        this.ColorInit(data.color);
        this.ClothesInit(data.data);
    }

    ColorInit(data: any) {
        let c: Color;
        if (data instanceof Color) {
            c = data;
            let personData = GameData.Instance.getRecordData("PersonData");
            personData[this.id].color = [c.r, c.g, c.b];
            GameData.Instance.setRecordData("PersonData", personData);
        }
        else c = color(data[0], data[1], data[2], 255);
        for (let i = 0; i < this.skins.length; i++) {
            const element = this.skins[i];
            element.color = c;
        }
    }

    ClothesInit(data) {
        let nums: [string, unknown][] = [...(Object.entries(data))];
        let urls: string[] = [];
        for (let i = 0; i < nums.length; i++) {
            const element = nums[i];
            if (element[0].includes("L") || element[0].includes("R")) urls.push(element[0].slice(1) + "/" + element[1]);
            else if (element[0] == "hair") {
                urls.push("F" + element[0] + "/" + element[1]);
                urls.push("B" + element[0] + "/" + element[1]);
            }
            else urls.push(element[0] + "/" + element[1]);
        }
        urls.push("sleeve/" + nums[2][1], "leg/" + nums[3][1]);
        for (let i = 0; i < urls.length; i++) {
            let element: string = urls[i];
            if ((element.charAt(element.length - 2) + element.charAt(element.length - 1)) != "-1") this.wears[i].spriteFrame = GameData.clothesBundle.get(element + "/spriteFrame", SpriteFrame);
            else this.wears[i].spriteFrame = null;
        }
        this.wears[19].spriteFrame = this.wears[16].spriteFrame;
        this.wears[20].spriteFrame = this.wears[17].spriteFrame;
        this.wears[21].spriteFrame = this.wears[18].spriteFrame;
    }

    Wear(wearType: CharacterWearType, id: number) {
        let personData = GameData.Instance.getRecordData("PersonData");
        switch (wearType) {
            case 0:
                personData[this.id].data.hair = id;
                break;
            case 1:
                personData[this.id].data.headwear = id;
                break;
            case 2:
                personData[this.id].data.up = id;
                break;
            case 3:
                personData[this.id].data.bottom = id;
                break;
            case 4:
                personData[this.id].data.eye = id;
                break;
            case 5:
                personData[this.id].data.mouth = id;
                break;
            case 6:
                personData[this.id].data.eyebrow = id;
                break;
            case 7:
                personData[this.id].data.belt = id;
                break;
            case 8:
                personData[this.id].data.cloak = id;
                break;
            case 9:
                personData[this.id].data.Learwear = id;
                break;
            case 10:
                personData[this.id].data.Rearwear = id;
                break;
            case 11:
                personData[this.id].data.Lhandwear = id;
                break;
            case 12:
                personData[this.id].data.Rhandwear = id;
                break;
            case 13:
                personData[this.id].data.necklace = id;
                break;
            case 14:
                personData[this.id].data.scarf = id;
                break;
            case 15:
                personData[this.id].data.shoe = id;
                break;
        }
        GameData.Instance.setRecordData("PersonData", personData);
        this.ClothesInit(personData[this.id].data);
    }

}